package com.test.javafaq_gateway.controller;

import org.bouncycastle.jcajce.provider.symmetric.TEA;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HomeController {

    //声明两个角色常量
    static final GrantedAuthority STUDENT = new SimpleGrantedAuthority("ROLE_STUDENT");
    static final GrantedAuthority TEACHER = new SimpleGrantedAuthority("ROLE_TEACHER");

    @GetMapping("/search.html")
    public String search(){
        return "search";
    }

    //不同身份的详情页
    @GetMapping("/question/detail.html")
    public String detail(@AuthenticationPrincipal UserDetails user){
        if(user.getAuthorities().contains(STUDENT)){
            return "question/detail";
        } else if (user.getAuthorities().contains(TEACHER)) {
            return "question/detail_teacher";
        }
        return null;
    }

    @GetMapping("/question/create.html")
    public String create(){
        return "question/create";
    }


    @GetMapping("/login.html")
    public ModelAndView loginForm(){
        return new ModelAndView("login");
    }

    @GetMapping("/index.html")
    public ModelAndView index(@AuthenticationPrincipal User user){
        //@AuthenticationPrincipal表示从springsecurity中获得用户信息
        if(user.getAuthorities().contains(STUDENT)){
            System.out.println("我是学生");
            return new ModelAndView("index");
        }else if(user.getAuthorities().contains(TEACHER)){
            System.out.println("我是讲师");
            return new ModelAndView("index_teacher");
        }
        return null;
    }




    // http://localhost:9000/register.html
    @GetMapping("/register.html")
    public String register(){
        return "register";
    }

}
